home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / rhythmbox / plugins / artdisplay / AmazonCoverArtSearch.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  7.8 KB  |  237 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from xml.dom import minidom
  5. import re
  6. import locale
  7. import urllib
  8. import rb
  9. import rhythmdb
  10. LICENSE_KEY = '18C3VZN9HCECM5G3HQG2'
  11. DEFAULT_LOCALE = 'en_US'
  12. ASSOCIATE = 'webservices-20'
  13. MAX_BATCH_JOBS = 2
  14.  
  15. class Bag:
  16.     pass
  17.  
  18.  
  19. class AmazonCoverArtSearch(object):
  20.     
  21.     def __init__(self):
  22.         self.searching = False
  23.         self.cancel = False
  24.         self.db = None
  25.         self.entry = None
  26.         (self.tld, self.encoding) = self._AmazonCoverArtSearch__get_locale()
  27.  
  28.     
  29.     def __get_locale(self):
  30.         supported_locales = {
  31.             'en_US': ('com', 'latin1'),
  32.             'en_GB': ('co.uk', 'latin1'),
  33.             'de': ('de', 'latin1'),
  34.             'ja': ('jp', 'utf8') }
  35.         lc_id = DEFAULT_LOCALE
  36.         default = locale.getdefaultlocale()[0]
  37.         if default:
  38.             if supported_locales.has_key(default):
  39.                 lc_id = default
  40.             else:
  41.                 lang = default.split('_')[0]
  42.                 if supported_locales.has_key(lang):
  43.                     lc_id = lang
  44.                 
  45.         
  46.         return supported_locales[lc_id]
  47.  
  48.     
  49.     def search(self, db, entry, on_search_completed_callback, *args):
  50.         self.searching = True
  51.         self.cancel = False
  52.         self.db = db
  53.         self.entry = entry
  54.         self.on_search_completed_callback = on_search_completed_callback
  55.         self.args = args
  56.         self.keywords = []
  57.         if not db.entry_get(entry, rhythmdb.PROP_ARTIST):
  58.             pass
  59.         st_artist = _('Unknown')
  60.         if not db.entry_get(entry, rhythmdb.PROP_ALBUM):
  61.             pass
  62.         st_album = _('Unknown')
  63.         if st_album == st_album:
  64.             pass
  65.         elif st_album == _('Unknown'):
  66.             self.on_search_completed(None)
  67.             return None
  68.         for char in [
  69.             '"']:
  70.             st_artist = st_artist.replace(char, '')
  71.             st_album = st_album.replace(char, '')
  72.         
  73.         self.st_album = st_album
  74.         self.st_artist = st_artist
  75.         for exp in [
  76.             '\\([Dd]isc *[1-9]+\\)',
  77.             '\\([Cc][Dd] *[1-9]+\\)']:
  78.             p = re.compile(exp)
  79.             st_album = p.sub('', st_album)
  80.         
  81.         st_album_no_vol = st_album
  82.         for exp in [
  83.             '\\(*[Vv]ol.*[1-9]+\\)*']:
  84.             p = re.compile(exp)
  85.             st_album_no_vol = p.sub('', st_album_no_vol)
  86.         
  87.         self.st_album_no_vol = st_album_no_vol
  88.         self.search_album = st_album
  89.         self.search_artist = st_artist
  90.         self.search_album_no_vol = st_album_no_vol
  91.         if st_album == _('Unknown'):
  92.             self.keywords.append('%s Best of' % st_artist)
  93.             self.keywords.append('%s Greatest Hits' % st_artist)
  94.             self.keywords.append('%s Essential' % st_artist)
  95.             self.keywords.append('%s Collection' % st_artist)
  96.             self.keywords.append('%s' % st_artist)
  97.         elif st_artist == _('Unknown'):
  98.             self.keywords.append('%s' % st_album)
  99.             if st_album_no_vol != st_artist:
  100.                 self.keywords.append('%s' % st_album_no_vol)
  101.             
  102.             self.keywords.append('Various %s' % st_album)
  103.         elif st_album != st_artist:
  104.             self.keywords.append('%s %s' % (st_artist, st_album))
  105.             if st_album_no_vol != st_album:
  106.                 self.keywords.append('%s %s' % (st_artist, st_album_no_vol))
  107.             
  108.             self.keywords.append('Various %s' % st_album)
  109.         
  110.         self.keywords.append('%s' % st_artist)
  111.         self.search_next()
  112.  
  113.     
  114.     def search_next(self):
  115.         if len(self.keywords) == 0:
  116.             self.on_search_completed(None)
  117.             return False
  118.         self.searching = True
  119.         url = 'http://ecs.amazonaws.' + self.tld + '/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=' + LICENSE_KEY + '&AssociateTag=' + ASSOCIATE + '&ResponseGroup=Images,ItemAttributes&Operation=ItemSearch&ItemSearch.Shared.SearchIndex=Music'
  120.         job = 1
  121.         while job <= MAX_BATCH_JOBS and len(self.keywords) > 0:
  122.             keyword = self.keywords.pop(0)
  123.             keyword = keyword.encode(self.encoding, 'ignore')
  124.             keyword = keyword.strip()
  125.             keyword = urllib.quote(keyword)
  126.             url += '&ItemSearch.%d.Keywords=%s' % (job, keyword)
  127.             job += 1
  128.             continue
  129.             len(self.keywords) == 0
  130.         l = rb.Loader()
  131.         l.get_url(url, self.on_search_response)
  132.         return True
  133.  
  134.     
  135.     def __unmarshal(self, element):
  136.         rc = Bag()
  137.         child_elements = _[1]
  138.         return rc
  139.  
  140.     
  141.     def on_search_response(self, result_data):
  142.         if result_data is None:
  143.             self.search_next()
  144.             return None
  145.         
  146.         try:
  147.             xmldoc = minidom.parseString(result_data)
  148.         except:
  149.             result_data is None
  150.             self.search_next()
  151.             return None
  152.  
  153.         data = self._AmazonCoverArtSearch__unmarshal(xmldoc)
  154.         if not hasattr(data, 'ItemSearchResponse') or not hasattr(data.ItemSearchResponse, 'Items'):
  155.             self.search_next()
  156.         else:
  157.             self.on_search_results(data.ItemSearchResponse.Items)
  158.  
  159.     
  160.     def on_search_results(self, results):
  161.         self.on_search_completed(results)
  162.  
  163.     
  164.     def on_search_completed(self, result):
  165.         self.on_search_completed_callback(self, self.entry, result, *self.args)
  166.         self.searching = False
  167.  
  168.     
  169.     def __tidy_up_string(self, s):
  170.         s = s.lower()
  171.         s = s.strip()
  172.         s = s.replace(' - ', ' ')
  173.         s = s.replace(': ', ' ')
  174.         s = s.replace(' & ', ' and ')
  175.         return s
  176.  
  177.     
  178.     def __valid_match(self, item):
  179.         if hasattr(item, 'LargeImage') or hasattr(item, 'MediumImage'):
  180.             pass
  181.         return hasattr(item, 'ItemAttributes')
  182.  
  183.     
  184.     def get_best_match_urls(self, search_results):
  185.         best_match = None
  186.         for result in search_results:
  187.             if not hasattr(result, 'Item'):
  188.                 continue
  189.             
  190.             items = filter(self._AmazonCoverArtSearch__valid_match, result.Item)
  191.             if self.search_album != _('Unknown'):
  192.                 album_check = self._AmazonCoverArtSearch__tidy_up_string(self.search_album)
  193.                 for item in items:
  194.                     if not hasattr(item.ItemAttributes, 'Title'):
  195.                         continue
  196.                     
  197.                     album = self._AmazonCoverArtSearch__tidy_up_string(item.ItemAttributes.Title)
  198.                     if album == album_check:
  199.                         best_match = item
  200.                         break
  201.                         continue
  202.                     if best_match is None:
  203.                         if album.find(album_check) != -1 or album_check.find(album) != -1:
  204.                             best_match = item
  205.                             continue
  206.                 
  207.             
  208.             if self.search_album == _('Unknown') and self.search_artist != _('Unknown'):
  209.                 artist_check = self._AmazonCoverArtSearch__tidy_up_string(self.search_artist)
  210.                 if best_match is None:
  211.                     hit = False
  212.                     for item in items:
  213.                         if not hasattr(item.ItemAttributes, 'Artist'):
  214.                             continue
  215.                         
  216.                         for artist in item.ItemAttributes.Artist:
  217.                             artist = self._AmazonCoverArtSearch__tidy_up_string(artist)
  218.                             if artist.find(artist_check) != -1:
  219.                                 best_match = item
  220.                                 hit = True
  221.                                 break
  222.                                 continue
  223.                         
  224.                         if hit:
  225.                             break
  226.                             continue
  227.                     
  228.                 
  229.             
  230.             urls = _[1]
  231.             if urls:
  232.                 return urls
  233.         
  234.         return []
  235.  
  236.  
  237.